package com.eveinfo.api.accessors;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.eveinfo.EveInfo;
import com.eveinfo.api.ApiException;
import com.eveinfo.api.NoCacheFoundExecption;
import com.eveinfo.api.utils.certificates.CertificatesParser;
import com.eveinfo.api.utils.certificates.CertificatesResponse;
import com.eveinfo.tools.EveInfoPreferences;
import com.eveinfo.tools.Tools;


public class Certificates {
	
	private static Certificates instance;
	
	private CertificatesResponse certificates;
		
	private Certificates()
			throws IOException, SAXException, ParserConfigurationException, ApiException, NoCacheFoundExecption {
		CertificatesParser parser = CertificatesParser.getInstance();
		final EveInfoPreferences preferences = EveInfo.getPreferences();
		if (preferences.isCacheOnly()) {
			parser.setCacheOnly(true);
			preferences.setEnableCache(true);
		} else {
			parser.setCacheOnly(false);
		}
		if (preferences.isEnableCache()) {
			Tools.setCacheEnable(parser);
		} else {
			Tools.setCacheDisable(parser);
		}
		certificates = parser.getCertificates();
		certificates.sort();
	}
	
	public static Certificates getInstance() throws IOException, SAXException, ParserConfigurationException, ApiException, NoCacheFoundExecption{
		if(instance == null){
			instance = new Certificates();
		}
		return instance;
	}

	public CertificatesResponse getCertificates() {
		return certificates;
	}
}
